/**
 * @author Linrui
 * @since 2011-03-15
 * 类别的数据持久化封装类，继承BaseDao并实现SortDao接口
 */
package com.qeado.sort.dao.ibatis;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.qeado.base.BaseDao;
import com.qeado.news.model.NewsDaoParameterMap;
import com.qeado.sort.dao.SortDao;
import com.qeado.sort.model.Sort;
import com.qeado.sort.model.SortDaoParameterMap;
import com.qeado.util.CommonConst;
import com.qeado.util.PageHelper;

@Repository
@SuppressWarnings("unchecked")
public class SortDaoImplement extends BaseDao implements SortDao {

	/* 查询所有商品一级类别 */
	public List<Sort> listTopCategory() {
		return (List<Sort>)this.getSqlMapClientTemplate().queryForList("Sort.listTopSort");
	}

	// 根据typeCode 查询它的下一级子类别
	public List<Sort> listByPcode(String typeCode) {
		return (List<Sort>) this.getSqlMapClientTemplate().queryForList(
				"Sort.listByCode", typeCode);
	}

	/* 根据父类id,查询它的下一级子类别 */
	public List<Sort> listByPid(String pid) {
		return (List<Sort>) this.getSqlMapClientTemplate().queryForList(
				"Sort.listByPid", pid);
	}	
	
	public Sort queryByCode(String typeCode) {
		return (Sort) this.getSqlMapClientTemplate().queryForObject(
				"Sort.queryByCode", typeCode);
	}

	public Sort queryById(String sortID) {
		return (Sort) this.getSqlMapClientTemplate().queryForObject(
				"Sort.queryById", sortID);
	}

	public PageHelper listByPcodeByPage(int pageNum, String Pcode) {
		PageHelper pageHelper = new PageHelper();
		pageHelper.setPageSize(CommonConst.getPageSizes());
		int startRecored = (pageNum - 1) * pageHelper.getPageSize();
		int pageSize = pageHelper.getPageSize();
		SortDaoParameterMap parameterMaps = new SortDaoParameterMap();
		parameterMaps.setPageSize(pageSize);
		parameterMaps.setStartRecored(startRecored);
		parameterMaps.setParameter1(Pcode);
		pageHelper.setObjList((List) this.getSqlMapClientTemplate()
				.queryForList("Sort.listByPcodeByPage", parameterMaps));
		pageHelper.setRecordCount((Integer) this.getSqlMapClientTemplate()
				.queryForObject("Sort.getCountByPcode", Pcode));
		return pageHelper;
	}

	public PageHelper listByPage(int pageNum) {
		PageHelper pageHelper = new PageHelper();
		pageHelper.setPageSize(CommonConst.getPageSizes());
		int startRecored = (pageNum - 1) * pageHelper.getPageSize();
		int pageSize = pageHelper.getPageSize();
		SortDaoParameterMap parameterMaps = new SortDaoParameterMap();
		parameterMaps.setPageSize(pageSize);
		parameterMaps.setStartRecored(startRecored);
		pageHelper.setObjList((List) this.getSqlMapClientTemplate()
				.queryForList("Sort.listByPage", parameterMaps));
		pageHelper.setRecordCount((Integer) this.getSqlMapClientTemplate()
				.queryForObject("Sort.getCount"));
		return pageHelper;
	}

	public PageHelper listByPidByPage(int pageNum, String Pid) {
		PageHelper pageHelper = new PageHelper();
		pageHelper.setPageSize(CommonConst.getPageSizes());
		int startRecored = (pageNum - 1) * pageHelper.getPageSize();
		int pageSize = pageHelper.getPageSize();
		SortDaoParameterMap parameterMaps = new SortDaoParameterMap();
		parameterMaps.setPageSize(pageSize);
		parameterMaps.setStartRecored(startRecored);
		parameterMaps.setParameter1(Pid);
		pageHelper.setObjList((List) this.getSqlMapClientTemplate()
				.queryForList("Sort.listByPidByPage", parameterMaps));
		pageHelper.setRecordCount((Integer) this.getSqlMapClientTemplate()
				.queryForObject("Sort.getCountByPid", Pid));
		return pageHelper;
	}

	public List<Sort> listByTreeByCode(String typeCode) {
		List<Sort> result = new ArrayList<Sort>();
		int count = (Integer) this.getSqlMapClientTemplate().queryForObject(
				"Sort.getCountByCode", typeCode);
		if (count > 0) {
			List<Sort> temp = (List<Sort>) this.getSqlMapClientTemplate()
					.queryForList("Sort.listByCode", typeCode);
			result.addAll(temp);
			/**
			 * 递归动态查询
			 **/
			for (Iterator iter = temp.iterator(); iter.hasNext();) {
				Sort sort = (Sort) iter.next();
				String sortId = sort.getId();
				result.addAll(listByTreeByPid(sortId));
			}

		} else {
			return new ArrayList<Sort>(); // 如果没有子节点了，则表示改节点已经是叶子节点了，则返回一个空的List
		}
		return result;
	}

	public List<Sort> listByTreeByPid(String Pid) {
		List<Sort> result = new ArrayList<Sort>();
		int count = (Integer) this.getSqlMapClientTemplate().queryForObject(
				"Sort.getCountByPid", Pid);
		if (count > 0) {
			List<Sort> temp = (List<Sort>) this.getSqlMapClientTemplate()
					.queryForList("Sort.listByPid", Pid);
			result.addAll(temp);
			/**
			 * 递归动态查询
			 **/
			for (Iterator iter = temp.iterator(); iter.hasNext();) {
				Sort sort = (Sort) iter.next();
				String sortId = sort.getId();
				result.addAll(listByTreeByPid(sortId));
			}
		} else {
			return new ArrayList<Sort>();
		}
		return result;
	}

	public PageHelper queryByName(int pageNum, String typeName) {
		// 暂时不实现
		return null;
	}

	public boolean isChildNode(String sortID) {
		Integer count = (Integer) this.getSqlMapClientTemplate()
				.queryForObject("Sort.getCountByPid", sortID);
		if (count == 0)
			return true; // 如果不存在子类，则当前类为叶子节点
		else
			return false;
	}

	public List<Sort> listLeafSortByPid(String sortID) {
		return (List<Sort>) this.getSqlMapClientTemplate().queryForList(
				"Sort.listLeafSortByPid", sortID);
	}

	public List<Sort> listLeafSortByPcode(String typeCode) {
		return (List<Sort>) this.getSqlMapClientTemplate().queryForList(
				"Sort.listLeafSortByCode", typeCode);
	}

	public String getNameByCode(String typeCode) {
		return (String)this.getSqlMapClientTemplate().queryForObject("Sort.getNameByCode", typeCode);
	}

	public List<Sort> listAllProdSort() {
		return (List<Sort>)this.getSqlMapClientTemplate().queryForList("Sort.listAllProductSort")   ;
	}	
	
}
